// pages/knowledge/knowledge.js
import * as echarts from "../../components/ec-canvas/echarts";
const httpApi = require("../../http/request"); //后续可以改为你自己的域名接口地址
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showWarn: false,
    ec: {
      lazyLoad: true, // 懒加载
    },
    dataList: [],
    echarCanve: null,
    has_num: 0,
    all_num: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData();
  },
  getData() {
    httpApi
      .Post("/index/getAtlas", { user_id: wx.getStorageSync("userInfo").id })
      .then((res) => {
        if (res.status) {
          return;
        }
        this.setData({
          dataList: res.data,
          showWarn: res.has_num < res.all_num,
          has_num: res.has_num,
          all_num: res.all_num,
        });
        this.initKonwledgeChart();
      });
  },
  initKonwledgeChart() {
    let knowledgeEchart = this.selectComponent("#knowledge-ec");
    knowledgeEchart.init((canvas, width, height, dpr) => {
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr, // new
      });

      chart.setOption(this.getOption());
      return chart;
    });
  },
  getOption() {
    // 格式化数据
    const nameList = this.data.dataList.map((item) => {
      return { name: item.name };
    });
    const echarsDataList = this.data.dataList.map((item) => {
      return item.my_point;
    });
    var options = {
      color: ["#FACD24"],
      xAxis: {
        show: false,
      },
      yAxis: {
        show: false,
      },
      radar: {
        nameGap: 10,
        indicator: nameList,
        axisName: {
          backgroundColor: "#25A7CD",
          color: "#ffffff",
          fontSize: 14,
          borderRadius: 20,
          padding: [4, 12],
        },
        axisLine: {
          lineStyle: {
            color: "#25A7CD",
          },
        },
        splitLine: {
          show: false,
        },
        splitArea: {
          show: true,
          areaStyle: {
            color: [
              "rgba(37, 167, 205, 0.7)",
              "rgba(37, 167, 205, 0.6)",
              "rgba(37, 167, 205, 0.5)",
              "rgba(37, 167, 205, 0.4)",
              "rgba(37, 167, 205, 0.3)",
              "rgba(37, 167, 205, 0.2)",
            ],
          },
        },
      },
      series: [
        {
          type: "radar",
          data: [
            {
              value: echarsDataList,
              name: "得分",
            },
          ],
        },
      ],
    };
    return options;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    var user=wx.getStorageSync("userInfo");
    var path='/pages/home/home?unitid='+user.unitid+'&unitid_copy='+user.unitid_copy;
    return {
      title: app.globalData.shareInfo[e.from].title,
      imageUrl: app.globalData.shareInfo[e.from].imageUrl,
      path: path,
      success: function(res) {
        // 转发成功
        console.log(11212);
      },
      fail: function(res) {
        // 转发失败
      }
    }
  },
});
